import { fetchHome } from '../../services/home/home';
import { fetchGoodsList } from '../../services/good/fetchGoods';
import { addToCart } from '../../services/cart/cart'
import Toast from 'tdesign-miniprogram/toast/index';
const app = getApp()

Page({
  data: {
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: { type: 'dots' },
    currentTab: null
  },

  goodListPagination: {
    index: 0,
    num: 20,
  },

  privateData: {
    tabIndex: 0,
  },

  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.init();
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();
  },

  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    fetchHome().then(({ swiper, tabList }) => {
      this.setData({
        tabList,
        imgSrcs: swiper,
        currentTab: tabList[0]._id
      });
      this.loadGoodsList(true);
    });
  },

  tabChangeHandle(e) {
    this.data.currentTab = e.detail.value
    this.loadGoodsList(true);
  },

  onReTry() {
    this.loadGoodsList();
  },

  loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({ goodsListLoadStatus: 1 });

    // const pageSize = this.goodListPagination.num || 0;
    // let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
    // if (fresh) {
    //   pageIndex = 0;
    // }

    try {
      // const nextList = fetchGoodsList(pageIndex, pageSize);
      this.setData({
        pageLoading: true,
        goodsList: []
      })
      wx.cloud.database().collection('book-list').where({
        filed: this.data.currentTab,
        state: true
      }).get({
        success:(res)=>{
          console.log(res);
          this.setData({
            goodsList: res.data,
            pageLoading: false
          });
        }
      })
      // this.setData({
      //   goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
      //   goodsListLoadStatus: 0,
      // });
      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({ goodsListLoadStatus: 3 });
    } finally {
      this.setData({
        pageLoading: false
      })
    }
  },

  goodListClickHandle(e) {
    const { index } = e.detail;
    const { _id } = this.data.goodsList[index];
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${_id}`,
    });
  },

  goodListAddCartHandle(e) {
    if(app.globalData.userInfo){
      addToCart(app.globalData.userInfo._id, e.detail.goods)
    }else{
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请先前往个人中心登录',
        duration: 1000,
      });
    }
  },

  navToSearchPage() {
    wx.navigateTo({ url: '/pages/goods/search/index' });
  },
});
